package com.xnfh.home.viewmodel

import com.xnfh.common.bean.HomeStatisticsNum
import com.xnfh.common.bean.WeatherDetailBean
import com.xnfh.common.constant.KvKey
import com.xnfh.framework.base.BaseViewModel
import com.xnfh.framework.bean.RespStateData
import com.xnfh.framework.utils.KVUtil
import com.xnfh.common.bean.WeatherWarningBean
import org.json.JSONObject

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/10 15:58
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/10 15:58
 * @UpdateRemark: 更新说明
 */
class HomeViewModel:  BaseViewModel() {

    var weatherWarningRes = RespStateData<List<WeatherWarningBean>>()
    var weatherRes = RespStateData<WeatherDetailBean>()
    var statisticsNum = RespStateData<HomeStatisticsNum>()

    private val homeRes = HomeRes()

    /**
     * 天气预警
     */
    fun requestWaetherWarning() = launch {
        val locationInfo = KVUtil.getString(KvKey.LOCATION_INFO)
        val jsonMes = JSONObject()
        jsonMes.put("lonlat",locationInfo)
        val requestBody = creatRequestBody(jsonMes.toString())
        homeRes.requestWaetherWarning(requestBody,weatherWarningRes)
    }

    /**
     * 天气状态
     */
    fun requestWeather() = launch {
        val locationInfo = KVUtil.getString(KvKey.LOCATION_INFO)
        homeRes.requestWeather(locationInfo.toString(),weatherRes)
    }

    /**
     * 首页统计地块和亩数
     */
    fun requestFiledStatistics() = launch {
        homeRes.requestFiledStatistics(statisticsNum)
    }
}